package com.ruoyi.shopping_products.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.shopping_products.domain.PdPdPool;
import com.ruoyi.shopping_products.mapper.PdUsersMapper;
import com.ruoyi.shopping_products.domain.PdUsers;
import com.ruoyi.shopping_products.service.IPdUsersService;

/**
 * 商城用户Service业务层处理
 * 
 * @author lzy
 * @date 2023-10-28
 */
@Service
public class PdUsersServiceImpl implements IPdUsersService 
{
    @Autowired
    private PdUsersMapper pdUsersMapper;

    /**
     * 查询商城用户
     * 
     * @param id 商城用户主键
     * @return 商城用户
     */
    @Override
    public PdUsers selectPdUsersById(Long id)
    {
        return pdUsersMapper.selectPdUsersById(id);
    }

    /**
     * 查询商城用户列表
     * 
     * @param pdUsers 商城用户
     * @return 商城用户
     */
    @Override
    public List<PdUsers> selectPdUsersList(PdUsers pdUsers)
    {
        return pdUsersMapper.selectPdUsersList(pdUsers);
    }

    /**
     * 新增商城用户
     * 
     * @param pdUsers 商城用户
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPdUsers(PdUsers pdUsers)
    {
        pdUsers.setCreateTime(DateUtils.getNowDate());
        int rows = pdUsersMapper.insertPdUsers(pdUsers);
        insertPdPdPool(pdUsers);
        return rows;
    }

    /**
     * 修改商城用户
     * 
     * @param pdUsers 商城用户
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePdUsers(PdUsers pdUsers)
    {
        pdUsers.setUpdateTime(DateUtils.getNowDate());
        pdUsersMapper.deletePdPdPoolByPoolId(pdUsers.getId());
        insertPdPdPool(pdUsers);
        return pdUsersMapper.updatePdUsers(pdUsers);
    }

    /**
     * 批量删除商城用户
     * 
     * @param ids 需要删除的商城用户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePdUsersByIds(Long[] ids)
    {
        pdUsersMapper.deletePdPdPoolByPoolIds(ids);
        return pdUsersMapper.deletePdUsersByIds(ids);
    }

    /**
     * 删除商城用户信息
     * 
     * @param id 商城用户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePdUsersById(Long id)
    {
        pdUsersMapper.deletePdPdPoolByPoolId(id);
        return pdUsersMapper.deletePdUsersById(id);
    }

    /**
     * 新增用户商品关联信息
     * 
     * @param pdUsers 商城用户对象
     */
    public void insertPdPdPool(PdUsers pdUsers)
    {
        List<PdPdPool> pdPdPoolList = pdUsers.getPdPdPoolList();
        Long id = pdUsers.getId();
        if (StringUtils.isNotNull(pdPdPoolList))
        {
            List<PdPdPool> list = new ArrayList<PdPdPool>();
            for (PdPdPool pdPdPool : pdPdPoolList)
            {
                pdPdPool.setPoolId(id);
                list.add(pdPdPool);
            }
            if (list.size() > 0)
            {
                pdUsersMapper.batchPdPdPool(list);
            }
        }
    }
}
